package com.doradance.view.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.doradance.util.CollectionUtils;
import com.doradance.view.listener.OnAdapterItemClickListener;
import com.doradance.view.listener.OnAdapterLongClickListener;
import com.doradance.view.listener.OnLoadMoreListener;
import com.doradance.view.status.StatusView;
import com.yong.android.framework.view.recyclerview.RecyclerViewCell;

import java.util.List;

/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

public abstract class SimpleCellListAdapter<VH extends RecyclerView.ViewHolder, ItemModel> {
    private DefaultRecyclerViewAdapter mAdapter;
    private DefaultCell cell;
    private boolean hasItemDecoration = false;
    protected View mHeaderView;

    public SimpleCellListAdapter(Context context) {
        mAdapter = new DefaultRecyclerViewAdapter(context);
        cell = new DefaultCell(this);
        mAdapter.addCell(cell);
    }

    public abstract VH onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType);

    public abstract void onBindViewHolder(VH holder, int position);

    public int getViewType(int position) {
        return position;
    }

    /**
     * 更新数据
     *
     * @param source
     */
    @SuppressWarnings("unchecked")
    public final void updateSource(List<ItemModel> source) {
        cell.list = null;
        if (mAdapter.isLoadMoreEnable()) {
            mAdapter.initPage();
            mAdapter.prepare();
        }
        cell.list = source == null ? null : (List<Object>) source;
        mAdapter.notifySuccess();
    }

    /**
     * 加载更多数据
     *
     * @param moreSource
     */
    public final void updateMore(List<ItemModel> moreSource) {
        if (mAdapter.isLoadMoreEnable()) {
            mAdapter.prepare();
            if (CollectionUtils.listSize(moreSource) != 0) {
                cell.list.addAll(moreSource);
            }
            mAdapter.notifySuccess();
        }

    }

    /**
     * 网络错误
     */
    public final void notifyFailure() {
        cell.list = null;
        mAdapter.notifyFailure();
    }

    /**
     * 加载更多失败
     */
    public final void notifyLoadMoreFailure() {
        mAdapter.notifyLoadMoreFailure();
    }

    public void notifyDataItemRemove(int position) {
        cell.list.remove(position);
        cell.notifyItemRemoved(position);
    }

    /**
     * 获取当前页数
     *
     * @return
     */
    public int getPage() {
        return mAdapter.getPage();
    }

    /**
     * 是否开启加载更多（若需要,请在初始化adapter开启）
     */
    public void openLoadMore() {
        mAdapter.openLoadMore();
    }


    public void addStatusView(StatusView statusView) {
        mAdapter.addStatusView(statusView);
    }



    /**
     * 上拉监听
     *
     * @param onLoadMoreListener
     */
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        mAdapter.setOnLoadMoreListener(onLoadMoreListener);
    }

    /**
     * 获取item
     *
     * @param position
     * @return
     */
    @SuppressWarnings("unchecked")
    public ItemModel getItem(int position) {
            return CollectionUtils.listSize(cell.list) == 0 ? null : (ItemModel) cell.list.get(position);
    }

    public void setItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        if (!hasItemDecoration) {
            mAdapter.addItemDecoration(itemDecoration);
            hasItemDecoration = true;
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
    }

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        mAdapter.setLayoutManager(layoutManager);
    }

    public int getPageSize() {
        return mAdapter.getPageSize();
    }

    public void setOnAdapterItemClickListener(OnAdapterItemClickListener onAdapterItemClickListener) {
        cell.setOnAdapterItemClickListener(onAdapterItemClickListener);
    }

    public void setOnAdapterLongClickListener(OnAdapterLongClickListener onAdapterLongClickListener) {
        cell.setOnAdapterLongClickListener(onAdapterLongClickListener);
    }


    public void notifyItemChanged(int position) {
        cell.notifyItemChanged(position);
    }

    public RecyclerView.Adapter getdapter() {
        return mAdapter;
    }

    private static class DefaultCell extends RecyclerViewCell<RecyclerView.ViewHolder> {
        private SimpleCellListAdapter mAdapter;
        private List<Object> list;

        private OnAdapterItemClickListener onAdapterItemClickListener;
        private OnAdapterLongClickListener onAdapterLongClickListener;

        public void setOnAdapterItemClickListener(OnAdapterItemClickListener onAdapterItemClickListener) {
            this.onAdapterItemClickListener = onAdapterItemClickListener;
        }

        public void setOnAdapterLongClickListener(OnAdapterLongClickListener onAdapterLongClickListener) {
            this.onAdapterLongClickListener = onAdapterLongClickListener;
        }

        public DefaultCell(SimpleCellListAdapter adapter) {
            this.mAdapter = adapter;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
            return mAdapter.onCreateViewHolder(inflater, parent, viewType);
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            mAdapter.onBindViewHolder(holder, position);
            if (onAdapterItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onAdapterItemClickListener.onAdapterItemClick(position);
                    }
                });
            }
            if (onAdapterLongClickListener != null) {
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        onAdapterLongClickListener.onAdapterLongItemClick(position);
                        return true;
                    }
                });
            }
        }

        @Override
        public int getCount() {
            return CollectionUtils.listSize(list);
        }

        @Override
        public int getItemViewType(int position) {
//            if(getItemViewType(position) == TYPE_HEADER) {
//                mAdapter.getViewType(position);
//            }
                return mAdapter.getViewType(position);
        }

    }


}
